<?php
namespace MyClass;
class Zbgg extends \MyClass\Common {
    //招标列表
    public function Show(){
			$condition = " 1=1";
			if($_REQUEST['city']) {
				$condition.=" AND inquirysheet.City = ".$_REQUEST['city'];
				$this->s('cityid',$_REQUEST['city']);
			}
			if($_REQUEST['cityname']){
				$this->s('cityname',$_REQUEST['cityname']);
			}
			if($_REQUEST['order']){
				if($_REQUEST['order'] ==1) {
					$ordertype = 'inquirysheet.Time desc';
				} else{
					$ordertype = 'inquirysheet.Time asc';
				}
				$this->s("order",$_REQUEST['order']);
			}else{
				$ordertype = 'inquirysheet.Time desc';
			}
			$condition.= " AND inquirysheet.Status=2";
	if(!$_SESSION['UserInfo']['id']){//未登录
			$condition.= " AND inquirysheet.Zbtype=1";
			//$p = new \Extend\Page($this->m('inquirysheet')->where($condition." AND inquirysheet.Status=2")->count(),15);
			$list = $this->m('inquirysheet')
				->join('company on inquirysheet.Cid=company.Id')
				->where($condition." AND inquirysheet.Status=2")
				->field('inquirysheet.*,company.CompanyName,company.CompanyAddress')
				->order("Time desc")
				->limit(6)
				->order($ordertype)
				->select();
			//$sedbid =  $this->m('inquirysheet')->where(array('Status'=> 2))->limit(10)->select();
			$this->s('list',$list);
			//$this->s('sedbid',$sedbid);
			//$this->s('page',$p->show());

	}else{//已登录
			if(!isset($_SESSION['UserInfo']['cid'])){//未完善信息只能查看公开的
				$condition.= " AND inquirysheet.Zbtype=1";
				//$p = new \Extend\Page($this->m('inquirysheet')->where($condition." AND inquirysheet.Status=2")->count(),15);
				$list = $this->m('inquirysheet')
					->join('company on inquirysheet.Cid=company.Id')
					->where($condition." AND inquirysheet.Status=2")
					->field('inquirysheet.*,company.CompanyName,company.CompanyAddress')
					->order("Time desc")
					->limit(6)
					->order($ordertype)
					->select();
				//$sedbid =  $this->m('inquirysheet')->where(array('Status'=> 2))->limit(10)->select();
				
			}else{
				$bus = $this->m("yaobiao")->where("Syid = ".$_SESSION['UserInfo']['cid'])->select();
			
				foreach($bus as $key=>$vo){
					$bgscid[] = $vo['Iid'];
				}
				
				if($bgscid>0){
					$bgscid = join(",",$bgscid);

				}else{
					$bgscid = 99999999999999;//暂无此数;
				}
				//$p = new \Extend\Page($this->m('inquirysheet')->where($condition." AND ((inquirysheet.Id in(".$bgscid.") And inquirysheet.Zbtype = 2) OR (inquirysheet.Cid = ".$_SESSION['UserInfo']['cid']." AND inquirysheet.Zbtype = 2)  OR inquirysheet.Zbtype = 1) AND (inquirysheet.Status =2)")->count(),15);
				//下面可以看到他本人的邀标和商友的邀标以及所有的公共投标 inquirysheet.Cid in(".$bgscid.") And inquirysheet.Zbtype = 2 //商友发的邀标
				$list = $this->m("inquirysheet")
					->join("company on inquirysheet.Cid=company.Id")
					->where($condition." AND ((inquirysheet.Id in(".$bgscid.") And inquirysheet.Zbtype = 2) OR (inquirysheet.Cid = ".$_SESSION['UserInfo']['cid']." AND inquirysheet.Zbtype = 2)  OR inquirysheet.Zbtype = 1)  AND (inquirysheet.Status=2)")
					->field('inquirysheet.*,company.CompanyName,company.CompanyAddress')
					->order("Time desc")
					->limit(6)
					->order($ordertype)
					->select();
			}
			//$sedbid =  $this->m('inquirysheet')->where(array('Status'=> 2))->limit(10)->select();
			//$this->s('sedbid',$sedbid);
			$this->s('list',$list);
			//$this->s('page',$p->show());
		}
       
        $this->v();
	}
    //详情
    public function Info(){
        if(!$_REQUEST['Id']) {
            $this->error('参数错误!');
        }
		$info = $this->m("inquirysheet")
				->join("company on inquirysheet.Cid = company.Id")
				->where("inquirysheet.Id=".$_REQUEST['Id'])
				->field("inquirysheet.*,company.CompanyName,company.CompanyAddress")->find();
		if(!$info){
			$this->error('请正确查看!');
		}
		$info['file'] = explode("/",trim($info['file'],"/"));
		$info['file'] = join(",",$info['file']);
		$zbproducts = $this->m("baojia")->where("Iid = ".$info['Id']." AND Status = 1")->find();
		$this->s("zbproducts",$zbproducts);
		$sedbid =  $this->m('inquirysheet')->where(array('Status'=> 2))->limit(10)->select();
		$this->s('sedbid',$sedbid);
        $this->s('info',$info);
        $this->s('state',$state);
        $this->v();
    }
    //添加报价
    //详情
    public function Add(){
	
        $save['Iid'] = $_REQUEST['Iid'];
        $save['Cid'] = $_REQUEST['Cid'];
        $save['Amount'] = $_REQUEST['total'];
        $save['Company'] = $_REQUEST['com'];
        $save['Phone'] = $_REQUEST['tel'];
        $save['Date'] = time();
        $save['File'] = $_REQUEST['files'][0];
		//$save['File']  = join("/",$_REQUEST['files']);
		//$save['File'] = $save['File'] ."/";
        $result = $this->m('baojia')->add($save);
        if($result) {
           $this->success('投标成功', T('Caigou/Show'));
        } else {
            $this->error('系统繁忙');
        }
    }
	public function PageAjax(){
			
		if(!isset($_REQUEST['page'])){
				$this->error("非法操作");
		}
		$thispage = (int)$_REQUEST['page']*6;
		//$_REQUEST['page'] = 1;
		$condition = " 1=1";
		if($_REQUEST['city']) {
				$condition.=" AND inquirysheet.City = ".$_REQUEST['city'];
				$this->s('cityid',$_REQUEST['city']);
		}
		if($_REQUEST['cityname']){
				$this->s('cityname',$_REQUEST['cityname']);
		}
		if($_REQUEST['order']){
			if($_REQUEST['order'] ==1) {
					$ordertype = 'inquirysheet.Time desc';
			} else{
					$ordertype = 'inquirysheet.Time asc';
			}
				$this->s("order",$_REQUEST['order']);
		}else{
				$ordertype = 'inquirysheet.Time desc';
		}
		$condition.= " AND inquirysheet.Status=2";

		if(!$_SESSION['UserInfo']['id']){//未登录
			$condition.= " AND inquirysheet.Zbtype=1";
			$count = $this->m('inquirysheet')->where($condition." AND inquirysheet.Status=2")->count();
			$list = $this->m('inquirysheet')
				->join('company on inquirysheet.Cid=company.Id')
				->where($condition." AND inquirysheet.Status=2")
				->field('inquirysheet.*,company.CompanyName,company.CompanyAddress')
				->order("Time desc")
				->limit($thispage.", 6")
				->order($ordertype)
				->select();
		}else{//已登录
			if(!isset($_SESSION['UserInfo']['cid'])){
				$this->error("请完善公司信息信息");
			}
			$bus = $this->m("yaobiao")->where("Syid = ".$_SESSION['UserInfo']['cid'])->select();
		
			foreach($bus as $key=>$vo){
				$bgscid[] = $vo['Iid'];
			}
			
			if($bgscid>0){
				$bgscid = join(",",$bgscid);

			}else{
				$bgscid = 99999999999999;//暂无此数;
			}
			$count =$this->m('inquirysheet')->where($condition." AND ((inquirysheet.Id in(".$bgscid.") And inquirysheet.Zbtype = 2) OR (inquirysheet.Cid = ".$_SESSION['UserInfo']['cid']." AND inquirysheet.Zbtype = 2)  OR inquirysheet.Zbtype = 1) AND (inquirysheet.Status =2)")->count(); 
			$list = $this->m("inquirysheet")
				->join("company on inquirysheet.Cid=company.Id")
				->where($condition." AND ((inquirysheet.Id in(".$bgscid.") And inquirysheet.Zbtype = 2) OR (inquirysheet.Cid = ".$_SESSION['UserInfo']['cid']." AND inquirysheet.Zbtype = 2)  OR inquirysheet.Zbtype = 1)  AND (inquirysheet.Status=2)")
				->field('inquirysheet.*,company.CompanyName,company.CompanyAddress')
				->order("Time desc")
				->limit($thispage.", 6")
				->order($ordertype)
				->select();
		}

		foreach($list as $key=>$v){
		
			$list[$key]['Time'] = date("Y-m-d",$v['Time']);
		}
		
		$ifhy = ceil($count/6);
		if((int)$_REQUEST['page']>$ifhy){
			$list['ifhy'] = 1;
		}
		echo json_encode($list);


	}


}